// 引入封装好的axios
import HttpServer from '@/utils/index'
import { type } from 'os';

// 登录
/*   export const user = <K>(data:{
    username:string,
    password:string
}) => HttpServer.request<typeof data,K>({
    method:'POST',
    url:'/admin/acl/index/login',
    data
})  */
interface successType<T = any> {
    code: number,
    message: string,
    data: T,
    success: boolean,
}
// 参数类型约束
type UserParams = {username:string; password:string} 
// 返回值类型约束
type UserResult = { code:number,data:{ token:string },message:string }
// 登录请求方法
export const userLogin = (data :UserParams )=>HttpServer.request<UserResult>({
    method:'POST',
    url:'/admin/acl/index/login',
    data
}) 

// 用户列表
// 参数类型
type userListS = {username?:object,page:number,limit:number}
// 返回数据类型
interface User {
    id: string;
    gmtCreate: string;
    gmtModified: string;
    deleted: boolean;
    username: string;
    password: string;
    nickName: string;
    salt: string | null;
    token: string | null;
    roleName: string;
}
interface UserList {
    total: number;
    items: User[];
}
export const userlist = ({username,page,limit}:userListS)=>HttpServer.request<successType<UserList>>({
    method:'GET',
    url:`/admin/acl/user/${page}/${limit}`,
    params:username,
})
// /admin/acl/user/save
// 用户添加 
type Useradd = {
    nickName: string;
    password: string;
    username: string;
}
export const userAdd = (data:Useradd)=>HttpServer.request<successType>({
    method:'POST',
    url:`/admin/acl/user/save`,
    data
})
// 批量删除 /batchRemove
export const deleteBatches = (data:string[])=>HttpServer.request<successType>({
    method:'post',
    url:`/admin/acl/user/batchRemove`,
    data
})
// 获取当前用户角色权限 /toAssign/id
interface userRole{
    id: string;
    gmtCreate: string;
    gmtModified: string;
    deleted: boolean;
    roleName: string;
    remark: string | null;
}
type Role = {
    assignRoles: userRole[];
    allRolesList: userRole[];
}
export const roleList = (userId:string)=>HttpServer.request<successType<Role>>({
    method:'GET',
    url:`/admin/acl/user/toAssign/${userId}`, 
})
//权限更新
interface queryParams {
    userId:string,
    userRoleIds:string[],
}
export const roleUpdate = ({userId,userRoleIds}:queryParams) => HttpServer.request<successType>({
    method:'post',
    url:`/admin/acl/user/doAssign?userId=${userId}&roleId=${userRoleIds}`
})

// 用户名修改 update
export interface userAlter {
    id: string;
    gmtCreate: string;
    gmtModified: string;
    deleted: boolean;
    username: string;
    password: string;
    nickName: string;
    salt: string | null;
    token: string | null;
    roleName: string;
} 
export const userUpdate= (params:userAlter)=> HttpServer.request<successType>({
    method:'put',
    url:`/admin/acl/user/update`,
    data:params, 
})
// 当前用户删除 remove/id
export const delUser = (id:string) => HttpServer.request<successType>({
    method:'delete',
    url:`/admin/acl/user/remove/${id}`
})
/* 
    角色权限模块
*/
// 角色列表 返回值
type roleList = {page:number,limit:number,searchObj:object}
export interface roleData {
    total: number;
    items: Item[];
} 
interface Item {
    id: string;
    gmtCreate: string;
    gmtModified: string;
    deleted: boolean;
    roleName: string;
    remark: string | null;
}
export const roleListAll = ({page,limit,searchObj}:roleList)=>HttpServer.request<successType<roleData>>({ 
    method:'get',
    url:`/admin/acl/role/${page}/${limit}`,
    params:searchObj,
})
// 添加角色
export const addRole = (role:object)=>HttpServer.request<successType>({
    method:'post',
    url:`/admin/acl/role/save`,
    data:role
})
// 批量删除
export const delRoleList = (roleL:string[])=>HttpServer.request<successType>({
    method:'post',
    url:`/admin/acl/role/batchRemove`,
    data:roleL
})
// 修改角色 update
type roleAmend = { id: string; roleName: string;}
export const amendRole = (role:roleAmend)=>HttpServer.request<successType>({
    method:'put',
    url:'/admin/acl/role/update',
    data:role
})
// 刪除角色 remove/${id}
export const delTheRole = (id:string)=>HttpServer.request<successType>({
    method:'delete',
    url:`/admin/acl/role/remove/${id}`,
})
// 获取当前角色权限列表 admin/acl/permission/toAssign
type TreeNode = {
    id: string;
    gmtCreate: string;
    gmtModified: string;
    deleted: boolean;
    pid: string;
    name: string;
    code: string;
    toCode: string;
    type: number;
    status?: string | null;
    level: number;
    children?: TreeNode;
    select: boolean;
} 
type Tree = TreeNode;
export const authList = (id:string|string[]) => HttpServer.request<successType<Tree>>({
    method:'get',
    url:`/admin/acl/permission/toAssign/${id}`
})
// 保存当前角色权限列表 doAssign
export const saveAuthId = (roleId:string|string[],permissionId:string)=>HttpServer.request<successType>({
    method:'post',
    url:'/admin/acl/permission/doAssign',
    params:{roleId,permissionId}
})
// 菜单列表 /admin/acl/permission
export const permissionList = ()=>HttpServer.request<successType<Tree>>({
    method:'get',
    url:'/admin/acl/permission'
})
// 新建菜单/admin/acl/permission/save
interface Per {
    level: number;
    name: string;
    code: string;
    toCode: string;
    pid: string;
    type: number;
}
export const addPerm = (permission:Per)=>HttpServer.request<successType>({
    method:'post',
    url:'/admin/acl/permission/save',
    data:permission
})